package com.xxl.job.admin;

import com.xxl.job.admin.core.model.XxlJobGroup;
import com.xxl.job.admin.core.model.XxlJobInfo;
import com.xxl.job.admin.core.model.XxlJobUser;
import com.xxl.job.admin.dao.XxlJobGroupDao;
import com.xxl.job.admin.dao.XxlJobInfoDao;
import com.xxl.job.admin.service.LoginService;
import com.xxl.job.admin.service.XxlJobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @Author: Zhihj
 * @Date: 2023-10-30 22:21:12
 */
@Controller
@RequestMapping("/jobinfo")
public class JobInfoController {
    @Resource
    XxlJobGroupDao xxlJobGroupDao;
    @Resource
    XxlJobService xxlJobService;

    @RequestMapping
    public String index(HttpServletRequest request, @RequestParam(required = false, defaultValue = "-1") int jobGroup) {
        List<XxlJobGroup> jobGroupList_all = xxlJobGroupDao.findAll();
        List<XxlJobGroup> jobGroupList = filterJobGroupByRole(request, jobGroupList_all);
        return "jobinfo/jobinfo.index";
    }

    /**
     * 该方法主要是过滤出来当前用户所拥有权限的X 权限
     *
     * @param request
     * @param jobGroupList_all
     * @return
     */
    private List<XxlJobGroup> filterJobGroupByRole(HttpServletRequest request, List<XxlJobGroup> jobGroupList_all) {
        List<XxlJobGroup> xxlJobGroups = new ArrayList<>();
        if (jobGroupList_all != null && jobGroupList_all.size() > 0) {
            //获取当前用户
            XxlJobUser loginUser = (XxlJobUser) request.getAttribute(LoginService.LOGIN_IDENTITY_KEY);
            if (loginUser.getRole() == 1) {
                xxlJobGroups = jobGroupList_all;
            } else {
                List<String> groupIdStrs = new ArrayList<>();//执行器ID列表
                String permission = loginUser.getPermission();
                if (permission != null && permission.trim().length() > 0) {
                    groupIdStrs = Arrays.asList(permission.trim().split(","));
                }
                for (XxlJobGroup xxlJobGroup : jobGroupList_all) {
                    if (groupIdStrs.contains(String.valueOf(xxlJobGroup.getId()))) {
                        xxlJobGroups.add(xxlJobGroup);
                    }
                }
            }
        }
        return xxlJobGroups;
    }

    @RequestMapping("/pageList")
    @ResponseBody
    public Map<String, Object> pageList(@RequestParam(required = false, defaultValue = "0") int start,
                                        @RequestParam(required = false, defaultValue = "10") int length,
                                        int jobGroup, int triggerStatus, String jobDesc, String executorHandler, String author) {

        return xxlJobService.pageList(start, length, jobGroup, triggerStatus, jobDesc, executorHandler, author);
    }
}
